//
//  SLRegisterController.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/12.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLRegisterController.h"
#import "AppDelegate.h"

@interface SLRegisterController ()

@property (weak, nonatomic) IBOutlet UITextField *userTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextFeild;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;

@end

@implementation SLRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        self.leftConstraint.constant = 23.0;
        self.rightConstraint.constant = 23.0;
    }
}

- (IBAction)registerBtnAction:(id)sender {
    [self.view endEditing:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if ([SLUtils isNilString:self.userTextFeild.text]) {
        [ProgressHUD showError:@"请输入手机号"];
        return;
    }
    if ([SLUtils isNilString:self.pwdTextFeild.text]) {
        [ProgressHUD showError:@"请输入注册密码"];
        return;
    }
    UserLocalInfo *userInfo = [UserLocalInfo sharedUserInfo];
    userInfo.userRegisterId = self.userTextFeild.text;
    userInfo.userRegisterPwd = self.pwdTextFeild.text;
    
    [XMPPManager sharedManager].registerOption = YES;
    __weak typeof(self) weakSelf = self;
    [[XMPPManager sharedManager] userRegitser:^(XMPPResultType type) {
        //放在主线程处理UI
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
            switch (type) {
                case XMPPResultTypeRegisterSuccess: {
                    userInfo.userId = weakSelf.userTextFeild.text;
                    userInfo.userPwd = weakSelf.pwdTextFeild.text;
                    if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(registerHadFinished)]) {
                        [weakSelf.delegate registerHadFinished];
                    }
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                }
                    break;
                case XMPPResultTypeRegisterFailure: {
                    [ProgressHUD showError:@"电话号码重复！"];
                }
                    break;
                case XMPPResultTypeNetError: {
                    [ProgressHUD showError:@"网络不给力啊！"];
                    break;
                }
                    
                default:
                    break;
            }
        });
    }];
}

- (IBAction)cancelRegister:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)textChanged:(id)sender {
    BOOL enable = (![SLUtils isNilString:self.userTextFeild.text] && ![SLUtils isNilString:self.pwdTextFeild.text]);
    self.registerBtn.enabled = enable;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
